package nieAS.ui {
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.SWFLoader;
	import com.greensock.TweenMax;
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	import nieAS.event.FLVEvent;
	import nieAS.video.FLVVideo;
	
	public class FLVPlayer extends FLVVideo {
		private static const UI_HEIGHT:int = 35;
		private static const BUTTON_HEIGHT:int = 12;
		private static const BUTTON_DIP:int = 20;
		private static const BG_COLOR:uint = 0x484848;
		private static const BUTTON_COLOR:uint = 0xA9A9A9;
		
		private var playerType:int;
		private var skinLoader:Loader;
		
		private var _ui:Sprite;
		private var _play_btn:Sprite;
		private var _pause_btn:Sprite;
		
		public function FLVPlayer($url:String = "", $width:Number = 320, $height:Number = 240, $scaling:Boolean = false, $playBack:Boolean = false, _type:int = 1):void {
			super($url, $width, $height, $scaling, $playBack);
			
			playerType = _type;
			
			/*var _loader:URLLoader = new URLLoader();
			_loader.dataFormat = URLLoaderDataFormat.BINARY;
			_loader.addEventListener(Event.COMPLETE, skinLoadedHandler);
			_loader.load(new URLRequest("skin.swf"));*/
		}
		
		private function skinLoadedHandler(evt:Event):void {
			/*var _ba:ByteArray = evt.target.data as ByteArray;
			skinLoader = new Loader();
			skinLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, function() {
				createUI();
				
				addEventListener(FLVEvent.FLV_PLAYING, playingHandler);
				addEventListener(FLVEvent.FLV_BEGIN, beginHandler);
			});
			skinLoader.loadBytes(_ba);*/
		}
		
		private function createUI():void {
			_ui = new Sprite();
			_ui.addChild(drawBG());
			_play_btn = drawPlayButton();
			_ui.addChild(_play_btn);
			_pause_btn = drawPauseButton();
			_ui.addChild(_pause_btn);
			_ui.x = 0;
			_ui.y = _height;
			this.addChild(_ui);
		}
		
		private function beginHandler(evt:FLVEvent):void {
			//trace(evt);
		}
		
		private function playingHandler(evt:FLVEvent):void {
			if (this._isPlaying) {
				TweenMax.to(_pause_btn, 0.5, { autoAlpha:1 } );
				TweenMax.to(_play_btn, 0.5, { autoAlpha:0 } );
			}else {
				TweenMax.to(_pause_btn, 0.5, { autoAlpha:0 } );
				TweenMax.to(_play_btn, 0.5, { autoAlpha:1 } );
			}
		}
		
		private function drawBG():Shape {
			var _bg:Shape = new Shape();
			_bg.graphics.beginFill(BG_COLOR, 1);
			_bg.graphics.drawRect(0, 0, _width, UI_HEIGHT);
			_bg.graphics.endFill();
			return _bg;
		}
		
		private function drawPlayButton():Sprite {
			var _btn:Sprite = new Sprite();
			_btn.addChild(drawButtonBG());
			_btn.x = _btn.y = (UI_HEIGHT - BUTTON_HEIGHT) / 2;
			_btn.graphics.beginFill(BUTTON_COLOR, 1);
			_btn.graphics.lineTo(BUTTON_HEIGHT / 2 * Math.sqrt(3), BUTTON_HEIGHT / 2);
			_btn.graphics.lineTo(0, BUTTON_HEIGHT);
			_btn.graphics.lineTo(0, 0);
			_btn.graphics.endFill();
			setOver(_btn);
			_btn.addEventListener(MouseEvent.CLICK, function() {
				resumeFLV();
			});
			return _btn;
		}
		
		private function drawPauseButton():Sprite {
			var _btn:Sprite = new Sprite();
			_btn.addChild(drawButtonBG());
			_btn.x = _btn.y = (UI_HEIGHT - BUTTON_HEIGHT) / 2;
			_btn.graphics.beginFill(BUTTON_COLOR, 1);
			_btn.graphics.drawRect(0, 0, BUTTON_HEIGHT / 2 - 1, BUTTON_HEIGHT);
			_btn.graphics.drawRect(BUTTON_HEIGHT / 2 + 1, 0, BUTTON_HEIGHT / 2 - 1, BUTTON_HEIGHT);
			_btn.graphics.endFill();
			setOver(_btn);
			_btn.addEventListener(MouseEvent.CLICK, function() {
				pauseFLV();
			});
			return _btn;
		}
		
		private function setOver(_tar:Sprite):void {
			_tar.buttonMode = true;
			_tar.alpha = 0.6;
			_tar.addEventListener(MouseEvent.MOUSE_OVER, function() {
				TweenMax.to(_tar, 0.5, { alpha:1 } );
			});
			_tar.addEventListener(MouseEvent.MOUSE_OUT, function() {
				TweenMax.to(_tar, 0.5, { alpha:0.6 } );
			});
		}
		
		private function drawButtonBG():Sprite {
			var _sp:Sprite = new Sprite();
			_sp.graphics.beginFill(0, 0);
			_sp.graphics.drawRect(0, 0, BUTTON_HEIGHT, BUTTON_HEIGHT);
			_sp.graphics.endFill();
			_sp.alpha = 0;
			return _sp;
		}
	}
}